//  
//  Copyright (C) 2009 Alex Allen
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using Gdk;
using System.ComponentModel;
using System.Xml;
using System.Xml.Serialization;

namespace twitalotgtk
{	
	public class Settings : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged Implementation
		 public event PropertyChangedEventHandler PropertyChanged;

	    private void NotifyPropertyChanged(String info)
	    {			
	        if (!_suppressPropertyChangedNotification && PropertyChanged != null)
	        {
	            PropertyChanged(this, new PropertyChangedEventArgs(info));
	        }
	    }
		#endregion

		
		#region Fields		
		private Color _windowColour = Color.Zero;
		private Color _rowColour = Color.Zero;
		private Color _altRowColour = new Color(168,168,168);
		private Color _rowTextColour = Color.Zero;
		private Color _altRowTextColour = Color.Zero;
		private Color _hyperLinkColour = Color.Zero;
		private Color _altHyperLinkColour = Color.Zero;
		private Color _inputBackgroundColour = Color.Zero;
		private Color _inputTextColour = Color.Zero;
		string _username = string.Empty;
		string _password = string.Empty;
		bool _storePassword = false;
		bool _relativeTime = true;
		
		protected bool _suppressPropertyChangedNotification = false;
		#endregion

		#region Public Properties
		public Color AltHyperLinkColour {
					get {
						return _altHyperLinkColour;
					}
					set {
						_altHyperLinkColour = value;
				NotifyPropertyChanged("AltHyperLinkColour");
					}
				}

		public Color AltRowColour {
					get {
						return _altRowColour;
					}
					set {
						_altRowColour = value;
				NotifyPropertyChanged("AltRowColour");
					}
				}

		public Color AltRowTextColour {
					get {
						return _altRowTextColour;
					}
					set {
						_altRowTextColour = value;
				NotifyPropertyChanged("AltRowTextColour");
					}
				}

		public Color HyperLinkColour {
					get {
						return _hyperLinkColour;
					}
					set {
						_hyperLinkColour = value;
				NotifyPropertyChanged("HyperLinkColour");
					}
				}

		public Color InputBackgroundColour {
					get {
						return _inputBackgroundColour;
					}
					set {
						_inputBackgroundColour = value;
				NotifyPropertyChanged("InputBackgroundColour");
					}
				}

		public Color InputTextColour {
					get {
						return _inputTextColour;
					}
					set {
						_inputTextColour = value;
				NotifyPropertyChanged("InputTextColour");
					}
				}

		public string Password {
					get {
						return _password;
					}
					set {
						_password = value;
				//NotifyPropertyChanged("Password");
					}
				}

		public Color RowColour {
					get {
						return _rowColour;
					}
					set {
						_rowColour = value;
				NotifyPropertyChanged("RowColour");
					}
				}

		public Color RowTextColour {
					get {
						return _rowTextColour;
					}
					set {
						_rowTextColour = value;
				NotifyPropertyChanged("RowTextColour");
					}
				}

		public bool StorePassword {
					get {
						return _storePassword;
					}
					set {
						_storePassword = value;
				NotifyPropertyChanged("SavePassword");
					}
				}

		public string Username {
					get {
						return _username;
					}
					set {
						_username = value;
				NotifyPropertyChanged("Username");
					}
				}

		public Color WindowColour {
					get {
						return _windowColour;
					}
					set {
						_windowColour = value;
				NotifyPropertyChanged("WindowColour");
					}
				}
		
		public bool RelativeTime
		{
			get { return _relativeTime;}
			set { _relativeTime = value; NotifyPropertyChanged("RelativeTime");}
		}
		
		#endregion

		public bool SuppressPropertyChangedNotification {
					get {
						return _suppressPropertyChangedNotification;
					}
					set {
						_suppressPropertyChangedNotification = value;
					}
				}		
		public Settings()
		{
		}
		
		private static readonly object syncLock = new object();
		
		public void Save(string path)
		{
			try
            {
                lock (syncLock)
                {
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    using (XmlWriter writer = XmlWriter.Create(path, settings))
                    {
						XmlSerializer serializer = new XmlSerializer(this.GetType());
						serializer.Serialize(writer, this);
                        writer.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                // Log the error
                System.Diagnostics.Debug.WriteLine(ex.Message);

                // Rethrow so we know about the error
                throw;
            }
		}
		
		public static Settings Load(string path)
		{
			if (!System.IO.File.Exists(path))
                return new Settings();

            lock (syncLock)
            {
                Settings settings;
                XmlReader xmlReader = XmlReader.Create(path);
				XmlSerializer serializer = new XmlSerializer(typeof(Settings));
				settings = serializer.Deserialize(xmlReader) as Settings;
                xmlReader.Close();
                return settings;
            }
		}
	}
}
